package cn.org.guhao.practice.go.thread;

/**
 *
 * Java程序演示如何在 Java 和 Java 中使用 notify 和 notifyAll 方法
 * 如何通知和 notifyAll 方法通知线程，哪个线程被唤醒等。
 *
 * @author guhao
 * @since 2020/5/7
 */
public class Notification {

    private volatile boolean go = false;

    /*
     * wait and notify can only be called from synchronized method or bock
     */
    public synchronized void shouldGo() throws InterruptedException {
      while(go != true){
        System.out.println(Thread.currentThread()
            + " is going to wait on this object");
        wait(); //release lock and reacquires on wakeup
        System.out.println(Thread.currentThread() + " is woken up");
      }
      go = false; //resetting condition
    }

    /*
     * both shouldGo() and go() are locked on current object referenced by "this" keyword
     */
    public synchronized void go() {
      while (go == false){
        System.out.println(Thread.currentThread()
            + " is going to notify all or one thread waiting on this object");

        go = true; //making condition true for waiting thread
        //notify(); // only one out of three waiting thread WT1, WT2,WT3 will woke up
        notifyAll(); // all waiting thread  WT1, WT2,WT3 will woke up
      }

    }



}
